﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Drawing;
using MySql.Data.MySqlClient;
using System.Windows.Forms;


namespace Database_Ass1_2013
{
    class DatabaseConnection
    {
        private string theConnectionString = "Server = 127.0.0.1; Port=3306; Username= root; password=; Database=TFBSUniversity";
        private MySqlDataReader aMySqlDataRead;
        private MySqlCommand aMySqlCommand;
        private MySqlConnection aConnection;

        public DatabaseConnection(string aQuery)
        {
            //create the connection
            aConnection = new MySqlConnection(theConnectionString);

            try
            {
                aConnection.Open(); //open the connection
                aMySqlCommand = aConnection.CreateCommand(); //create the execution command
                aMySqlCommand.CommandText = aQuery; //assign to the commend the query to be done 

                aMySqlDataRead = aMySqlCommand.ExecuteReader(); //execute the reader (it is forward only)//
            }
            catch (Exception ex)
            {
                //show up the message in case of failure
                MessageBox.Show("Command query execution failure due to:\n\n" + ex.ToString());
                throw;
            }
        }

        
        public MySqlDataReader getDataReader()
        {
            return aMySqlDataRead;
        }

        public MySqlCommand getMySqlCommand()
        {
            return aMySqlCommand;
        }

        public MySqlConnection getMySqlConnection()
        {
            return aConnection;
        }
    }
}
